<?php
/**
 * @Created on 2020/01/15
 * @Author QQ 1026269609@qq.com
*/
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Index extends Base
{
    public function index()
    {
//        halt(getrewardcount(936));
		$data=db('auth_rule')->where(['pid'=>0,'status'=>1])->order('sort ASC')->select();
		foreach($data as $k=>$v){
		    //无权限的菜单不显示
			if(!get_auth($v['name'],session('uid')) && session('uid') != 1){
				unset($data[$k]);
			}else{
				$data[$k]['sub']=db('auth_rule')->where(['pid'=>$v['id'],'status'=>1])->order('sort ASC')->select();
				foreach($data[$k]['sub'] as $k2=>$v2){
                    $nameArr=explode('?',$v2['name']);
                    $name=$nameArr[0];
					if(!get_auth($name,session('uid')) && session('uid') != 1){
						unset($data[$k]['sub'][$k2]);
					}
				}
			}
		}
		$this->assign('data',$data);
		$this->assign('username',session('username'));
		$url=str_replace('/index.php' ,'' ,$_SERVER['SCRIPT_NAME']);
		$this->assign('url',$url);
        return $this->fetch();
    }
    public function password(){
        //修改密码
    	if(request()->isPost()){
			$password=request()->param('password');
			$info=db('member')->where(['uid'=>session('uid'),'password'=>md5($password)])->find();
			if($info){
				return json(['status'=>0,'message'=>'新密码与旧密码相同，请重新修改']);
			}
			db('member')->where('uid',session('uid'))->update(['password'=>md5($password)]);
			return json(['status'=>1,'message'=>'修改成功']);
		}else{
			$this->assign('username',session('username'));
			return $this->fetch();
		}
    }
    public function welcome(){//欢迎页面
    	$this->assign('login_time',session('login_time'));
		$this->assign('login_ip',session('login_ip'));
		$this->assign('username',session('username'));
		$session_uid=session('uid');
		//欢迎
		$group=db('auth_group')->where('id',session('roleid'))->find();
		$this->assign('group',$group);

		//登录信息
		$login_log=Db::query("select a.title,b.username,b.login_ip,b.login_time,b.remark from dsp_auth_group AS a,dsp_member_login_log AS b where a.id=b.roleid and b.uid={$session_uid} order by b.id desc limit 5");
		$this->assign('login_log',$login_log);
        return $this->fetch();
    }

}